package com.breezelin.freshmarket.adpaters;

/**
 * Created by Breeze Lin
 * 2016/5/31 15:22
 * breezesummerlin@163.com
 */

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 动态图片内容的适配器
 */
public class PicScanAdapter extends PagerAdapter implements View.OnClickListener {

	private Context mContext;
	private List<String> mContents;
	private MomentPicListener mListener;

	public PicScanAdapter(Context context, List<String> contents, MomentPicListener listener) {
		mContext = context;
		mContents = contents;
		mListener = listener;
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {

		// 创建图片控件
		ImageView imgView = new ImageView(mContext);
		imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
		Picasso.with(mContext).load(mContents.get(position)).into(imgView);
		container.addView(imgView);

		// 设置监听
		imgView.setTag(position);
		imgView.setOnClickListener(this);
		return imgView;
	}

	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		container.removeView((View) object);
	}

	@Override
	public int getCount() {
		int count = 0;
		if (mContents != null) {
			count = mContents.size();
		}
		return count;
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == object;
	}

	@Override
	public void onClick(View v) {
		mListener.onPicClick((int) v.getTag());
	}

	/**
	 * 动态图片监听
	 */
	public interface MomentPicListener {

		/**
		 * 图片点击回调
		 *
		 * @param position 图片位置
		 */
		void onPicClick(int position);
	}
}
